package com.wisdom.benefit.jufa.product;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wisdom.benefit.jufa.event.CallBackInterface;
import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.bean.HomeListBean;
import com.wisdom.benefit.jufa.widget.SwitchButton;

import java.util.ArrayList;

public class AccountAdapter extends BaseAdapter {
    private ArrayList<HomeListBean> datas;
    private Context context;
    private CallBackInterface callback; //回调接口

    public AccountAdapter(ArrayList<HomeListBean> datas, Context context, CallBackInterface callback) {
        this.datas = datas;
        this.context = context;
        this.callback = callback;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AccountAdapter.ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.account_item, parent, false);
            holder = new AccountAdapter.ViewHolder();
            holder.nameTv = (TextView) convertView.findViewById(R.id.account_item_tv);
            holder.iv = (ImageView) convertView.findViewById(R.id.account_item_iv);
            holder.sBtn = (SwitchButton) convertView.findViewById(R.id.account_item_sBtn);
            holder.btn = (Button) convertView.findViewById(R.id.account_item_btn);
            convertView.setTag(holder);
        } else {
            holder = (AccountAdapter.ViewHolder) convertView.getTag();
        }
        if (datas.get(position).getPassagewayType() == 1) {
            holder.iv.setImageResource(R.mipmap.ali_icon);
        } else if (datas.get(position).getPassagewayType() == 3) {
            holder.iv.setImageResource(R.mipmap.transfer_accounts);
        } else if (datas.get(position).getPassagewayType() == 2) {
            holder.iv.setImageResource(R.mipmap.wechat_icon);
        }else if (datas.get(position).getPassagewayType() == 4) {
            holder.iv.setImageResource(R.mipmap.zhikouling);
        }
        if (datas.get(position).getStatus() == 1) {
            holder.sBtn.setChecked(true);
            if (TextUtils.isEmpty(datas.get(position).getPaymentCodePayeeName())) {
                holder.nameTv.setText("无收款码");
            } else {
                holder.nameTv.setText(datas.get(position).getPaymentCodePayeeName());
            }
        } else {
            holder.nameTv.setText("非收款账户");
            holder.sBtn.setChecked(false);
        }
        if (datas.get(position).getStatus() == 1) {
            holder.btn.setEnabled(true);
        } else {
            holder.btn.setEnabled(false);
        }
        holder.sBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    callback.callBackClickOther(position, false);
                }

        });
// setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
//
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
////                if (isChecked){
////                    callback.callBackClickOther(position,true);
////                }else{
////                    callback.callBackClickOther(position,false);
////                }
//            }
//        });
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.callBackClick(position);
            }
        });
        holder.btn.setTag(position);
        holder.sBtn.setTag(position);
        return convertView;
    }

    final static class ViewHolder {
        ImageView iv;
        TextView nameTv;
        SwitchButton sBtn;
        Button btn;
    }
}

